/**
 * Desc :
 * Created by byte2 on 2016-12-09.
 */
'use strict';


var path = require("path");
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");

function isDebugger() {
    return true;
    return process.env.NODE_ENV.trim() == 'development';
}
var plugins = [
    new webpack.ProvidePlugin({
        React: 'react',
        // React: path.resolve(node_modules_dir,
        //     'react/libs/ReactWithAddons.js'),
        ReactDOM: 'react-dom',
        // RouteTransition: 'react-router-transition',
        _: 'underscore',
        Moment: 'moment',

        // $: "jquery",
        // jQuery: "jquery",
        // "window.jQuery": "jquery",
        // "window.$": "jquery",
        // $: 'jquery',
        // jQuery: 'jquery',
    }),
    new ExtractTextPlugin('css/styles.css'),
    new webpack.DllReferencePlugin({
        context: __dirname,
        manifest: require('./build/vendor-manifest.json')
    })
];

var entry = {
    app: ["./app"]
};
if (!isDebugger()) { //production mode
    var HtmlWebpackPlugin = require('html-webpack-plugin');

    plugins = plugins.concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: JSON.stringify('production')
            }
        }),
        //js 的压缩
        new webpack.optimize.UglifyJsPlugin({
            test: /(\.jsx|\.js)$/,
            exclude: /node_modules/,
            compress: {
                warnings: false
            },
            sourceMap: false,
        }),
        new HtmlWebpackPlugin({
            title: '安道云科',
            filename: 'index.html',
            hash: true,
            template: 'app/index.ejs'
        }),
    ]);
} else { // development mode
    plugins = plugins.concat([
        new webpack.HotModuleReplacementPlugin(), //hot module replacement
    ]);
}
var node_modules_dir = path.join(__dirname, 'node_modules');

module.exports = {
    entry: entry,
    target: 'web',
    devServer: {
        inline: true,
    },
    output: {
        path: path.resolve(__dirname, "build"),
        publicPath: "/",
        // publicPath: "/assets/",
        filename: "bundle.js"
    },
    devtool: isDebugger() ? 'source-map' : '',
    plugins: plugins,
    module: {
        loaders: [
            // {
            //     test: require.resolve('jquery'),
            //     loader: 'expose?$!expose?jQuery'
            // },
            {
                test: /\.css$/,
                // exclude: /node_modules/,
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap')
            },
            {
                test: /\.(less)$/,
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader?sourceMap")
            },
            {
                test: /\.(js|jsx)$/,
                loader: 'babel',
                exclude: /node_modules/,
                query: {
                    plugins: [
                        "transform-es3-property-literals",
                        "transform-es3-member-expression-literals",
                        "transform-proto-to-assign",
                        ["transform-es2015-classes", {
                            "loose": true
                        }]
                    ],
                    presets: ['stage-0', 'react', 'es2015']
                },
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/,
                loader: 'file-loader?limit=1024&name=images/[name].[ext]'
            },
            {
                test: /\.(woff|woff2)/,
                loader: 'file-loader?limit=100000&name=fonts/[name].[ext]'
            },
            {
                test: /\.(eot|ttf)/,
                loader: 'file-loader?name=fonts/[name].[ext]'
            },
            {
                test: /\.(svg)/,
                loader: 'file-loader?name=fonts/[name].[ext]'
            },
        ]
    },
    // resolveLoader: {
    // root: path.join(__dirname, 'node_modules')
    // },
    context: path.resolve(__dirname),
    resolve: {
        root: [
            path.resolve(__dirname, 'app'),
            path.resolve(__dirname, 'node_modules'),
            path.resolve(__dirname, 'bower_components')
        ],
        // extensions: ['.css', '.js'],
        //配置别名，在项目中可以缩减引用路径
        alias: {
            'jquery': "jquery/src/jquery",//for webpack it is better to alias the src version because this way webpack is able to optimize dependencies better
            // 'react': path.resolve(node_modules_dir,
            //     'react/libs/ReactWithAddons.js'),
            'isotope_': "libs/isotope/isotope.pkgd.js",
            'react-router-transition': path.resolve(node_modules_dir,
                'react-router-transition/lib/react-router-transition.js')
            // jquery: path.resolve(__dirname, './examples.react/libs/jquery.js'),
            // ui: srcDir + '/js/ui',
            // libs: path.resolve(srcDir, './../../libs'),
        }
    },
};